/**
 * Created by hjqi on 2016/6/8.
 */
module.controller('ClusterAddNodeController',
    function($scope, $rootScope,$state, $stateParams, $q,clusterService,VALUE){


        var clusterId = $stateParams.clusterId;
        //token will load by db

        $scope.token = '123';
        $scope.env = VALUE.ENV;

        $scope.myPromise = undefined;
        $scope.templateUrl = '/template/cp/ngbusytemplate.html';


        $scope.modal = {};
        $scope.modal.steps = [
            {num:1,title:'安装 docker'},
            {num:2,title:'安装 agent'}
        ];
        $scope.modal.step = 0;

        loadToken();

        $scope.isFirstStep = function () {
            return $scope.modal.step === 0;
        };

        $scope.isLastStep = function () {
            return $scope.modal.step === ($scope.modal.steps.length - 1);
        };

        $scope.isCurrentStep = function (step) {
            return $scope.modal.step === step;
        };

        $scope.setCurrentStep = function (step) {
            if(!$scope.lockStep)
                $scope.modal.step = step;
        };

        $scope.getCurrentStep = function () {
            return $scope.modal.steps[$scope.modal.step].num;
        };

        $scope.getNextLabel = function () {
            var _lastLabel = '添加';
            return ($scope.isLastStep()) ? _lastLabel : '下一步';
        };

        $scope.handlePrevious = function () {
            if(!$scope.lockStep){
                if($scope.isFirstStep())
                    $state.go('cp.clusterdetail',{clusterId:clusterId});
                else
                    $scope.modal.step -= 1;
            }
        };

        $scope.handleNext = function () {
            if ($scope.isLastStep()) {
                //need to create service and go back to service page

                angular.element(".spinner")
                    .fadeIn("fast")
                    .delay(1000000)
                    .fadeOut('slow', function() {
                        //

                    });

                var testPromise = $q.defer();
                $scope.myPromise = testPromise.promise;

                //TODO wait  node add
                waitNodeAdded(testPromise);


            } else {

                $scope.modal.step += 1;
            }
        };

        $scope.$on('$destroy', function(){

            //unsubscribe event


        });

        function loadToken(){

            clusterService.getClusterToken(clusterId).then(function(ret){
                if(ret.data.ret === true){
                    $scope.token = ret.data.data;
                }else{
                    alertify.alert('cluster get  token error happened;'+ret.data.data);
                }
            }).catch(function(err){
                alertify.alert('cluster get  token error happened;'+err);
            });

        }

        function waitNodeAdded(_promise){

            clusterService.checkNode(clusterId,$scope.token).then(function(ret){
                //
                if(ret.data.ret === true){
                    _promise.resolve();
                    //go to detail page
                    $state.go('cp.clusterdetail',{clusterId:clusterId});
                }else{
                    setTimeout(waitNodeAdded,2000,_promise);
                }
            }).catch(function(err){
                alertify.alert('cluster get node error happened;'+err);
                //retry
                setTimeout(waitNodeAdded,2000,_promise);
            });

        }

        //'<dd ng-class="select==$index?"active":""" ng-click="open($index,data.child)" ><span>ddd</span></dd>'


});